<template>
    <view class="content">
		<!-- 商品轮播图 -->
		<u-swiper :list="goodsInfo.rotationalPic" mode="number" indicator-pos="bottomRight" height="750" border-radius="0rpx"></u-swiper>
		
		<!-- 商品详细信息 -->
		<view class="goods-info">
			<view class="top">
				<view class="goods-name">{{ goodsInfo.title }}</view>
				<view class="goods-price">
					<view class="left">
						<text class="group">拼团价</text>
						<text class="symbol">¥<text class="text">{{ goodsInfo.groupPrice || 0 }}</text></text>
						<text class="old">{{ goodsInfo.price || 0 }}</text>
					</view>
					<view class="right">
						<text class="text">{{ groupType === 1 ? '开' : '参' }}团倒计时</text>
						<u-count-down :timestamp="groupTime" :show-days="false" color="#FF1A2E" separator-color="#FF1A2E" bg-color="#FFF9DE"></u-count-down>
					</view>
				</view>
			</view>
			<view class="deserved-money">
				<image class="image" src="@/static/images/goods/red_package.png"></image>
				<text class="text">未拼中奖励{{ goodsInfo.deservedMoney || 0 }}元红包</text>
			</view>
		</view>
		
		<!-- 拼团玩法 -->
		<view class="section-module">
			<view class="header">
				<view class="left">拼团玩法</view>
				<view class="right"></view>
			</view>
			<view class="section-content">
				<image class="rule" src="@/static/images/goods/group_rule.png"></image>
			</view>
		</view>
		
		<!-- 拼团详情 -->
		<view class="section-module">
			<view class="header">
				<view class="left">
					已有
					<text class="text" v-if="goodsInfo.newPerson === 1 && groupInfo.participated === 1">
						<text v-if="groupInfo.status === 2">{{ goodsInfo.groupUserNumber }}</text>
						<text v-else>{{ goodsInfo.alreadyGroupUserNumber + 1 }}</text>
					</text>
					<text class="text" v-else>{{ goodsInfo.alreadyGroupUserNumber }}</text>
					人成团
				</view>
				<view class="right" @tap="toGroupDetail()">
					查看拼团成员
					<u-icon name="arrow-right"></u-icon>
				</view>
			</view>
			<view class="section-content">
				<view class="left">
					<template v-for="(pic, picIndex) in goodsInfo.usersInfo">
						<image class="image" :src="pic.headImg" v-if="picIndex < 6"></image>
					</template>
				</view>
				<view class="right">
					仅差
					<text class="text" v-if="goodsInfo.newPerson === 1 && groupInfo.participated === 1">
						<text v-if="groupInfo.status === 2">0</text>
						<text v-else>{{ goodsInfo.groupUserNumber - goodsInfo.alreadyGroupUserNumber - 1 }}</text>
					</text>
					<text class="text" v-else>{{ goodsInfo.groupUserNumber - goodsInfo.alreadyGroupUserNumber }}</text>
					人成团
				</view>
			</view>
		</view>
		
		<!-- 商品详情图 -->
		<view class="derail-msg" v-html="goodsInfo.detailMsg"></view>
		
		<!-- 底部固定栏 -->
		<view class="footer-bar">
			<button class="btn join" @tap="joinGroup">{{ groupInfo.participated === 1 ? '已参团' : '参加拼团' }}</button>
		</view>
		
		<!-- 回到顶部 -->
		<u-back-top :scroll-top="scrollTop" top="10"></u-back-top>
	</view>
</template>

<script>
	import { mapGetters } from 'vuex';
	import moment from '@/utils/packges/moment'
    export default {
        name: "detail",
		data() {
			return {
				options: {},
				goodsInfo: {},
				groupInfo: {},
				groupTime: 0,
				groupType: 1,
				scrollTop: 0
			}
		},
		onLoad(options) {
			this.options = options;
		},
		onShow() {
			this.initPage();

			// #ifdef MP-WEIXIN
			uni.reportAnalytics('enter_shangpinxq');
			// #endif
		},
		computed: {
			...mapGetters(['userInfo'])
		},
		methods: {
			// 初始化页面
			initPage() {
				uni.showLoading({
					title: '加载中...'
				})
				// 获取商品详情
				this.$service.shopping.goods.info({
					id: this.options.gid
				}).then(res => {
					res.rotationalPic = res.rotationalPic.split(',');
					this.goodsInfo = res;
					
					// 计算剩余时长
					if (parseInt(res.startTime  / 1000) - parseInt(moment().format('X')) > 0) {
						this.groupTime = parseInt(res.startTime / 1000) - parseInt(moment().format('X'))
						this.groupType = 0;
					} else {
						this.groupTime = parseInt(res.endTime / 1000) - parseInt(moment().format('X'))
						this.groupType = 1;
					}
					
					// if (res.status === 1 && res.newPerson === 1) {
					// 	this.groupType = 1;
					// 	this.groupTime = 1800;
					// }
					
					if (res.isOpened === 1) {
						this.groupTime = 0;
					}
					
					if (this.goodsInfo.isAddRobot === 1 && this.goodsInfo.newPerson === 0) {
						this.goodsInfo.alreadyGroupUserNumber = this.goodsInfo.alreadyGroupUserNumber + this.goodsInfo.groupRobotUserNumber
					}
					
					if (this.goodsInfo.isAddRobot === 1 && this.goodsInfo.newPerson === 1) {
						this.goodsInfo.alreadyGroupUserNumber = this.goodsInfo.groupRobotUserNumber;
					}
					
					this.goodsInfo.detailMsg = this.formatRichText(this.goodsInfo.detailMsg);
					
					uni.hideLoading();
					
					if (uni.getStorageSync('token')) {
						// 获取参团人员详情
						this.$service.order.combination.usersInfo({
							combinationId: this.options.gid
						}).then(res => {
							this.groupInfo = res;
							
							if (res.participated === 1 && this.goodsInfo.newPerson === 1) {
								this.goodsInfo.usersInfo.push({
									username: this.userInfo.username,
									headImg: this.userInfo.headImg,
								})
							}
						});
					}
				});
			},
			// 查看拼团详情
			toGroupDetail() {
				uni.navigateTo({
					url: '/pages/group/detail?gid=' + this.options.gid
				});
				
				// #ifdef MP-WEIXIN
				uni.reportAnalytics('click_chakanpintuanxq');
				// #endif
			},
			// 参加拼团
			joinGroup() {
				// #ifdef MP-WEIXIN
				uni.reportAnalytics('click_canyupt_sp');
				// #endif
				
				if (!uni.getStorageSync('token')) {
					if (this.goodsInfo.newPerson === 1) {
						uni.navigateTo({
							url: '/pages/sign/login?redirectUrl=reLaunch'
						});
					} else {
						uni.navigateTo({
							url: '/pages/sign/login?redirectUrl=navigateBack'
						});
					}
					return false;
				}
				if (this.groupInfo.participated === 1) {
					uni.showToast({
						title: '您已参团',
						icon: 'none'
					});
					return false;
				}
				if (parseInt(this.goodsInfo.startTime  / 1000) - parseInt(moment().format('X')) > 0) {
					uni.showToast({
						title: '抱歉，还没到参团时间！',
						icon: 'none'
					});
					return false;
				}
				uni.navigateTo({
					url: '/pages/order/settlement?gid=' + this.options.gid
				})
			},
			formatRichText(html) {
				let newContent;
			// 	// 替换标签
			// 	newContent = html.replace(/<p>(.*?)<\/p>/g, '<view>$1</view>');
				newContent = html.replace(/<img(.*?)>/g, '<img style="width:100%;display:block" $1 />');
			// 	newContent = newContent.replace(/<font color="(.*?)<\/font>/g, '<text class="text" style="color: $1</text>');
			// 	newContent = newContent.replace(/<i>(.*?)<\/i>/g, '<text class="text">$1</text>');
			// 	newContent = newContent.replace(/<span(.*?)<\/span>/g, '<view class="text"$1</view>');
			// 	newContent = newContent.replace(/<h1 style="(.*?)<\/h1>/g, '<view style="display:block;$1</view>');
			// 	newContent = newContent.replace(/<h2 style="(.*?)<\/h2>/g, '<view style="display:block;$1</view>');
			// 	newContent = newContent.replace(/<h3 style="(.*?)<\/h3>/g, '<view style="display:block;$1</view>');
			// 	newContent = newContent.replace(/<h4 style="(.*?)<\/h4>/g, '<view style="display:block;$1</view>');
			// 	newContent = newContent.replace(/<h5 style="(.*?)<\/h5>/g, '<view style="display:block;$1</view>');
			// 	newContent = newContent.replace(/<h6 style="(.*?)<\/h6>/g, '<view style="display:block;$1</view>');
			// 	newContent = newContent.replace(/<h7 style="(.*?)<\/h7>/g, '<view style="display:block;$1</view>');
			// 	newContent = newContent.replace(/<p style="(.*?)<\/p>/g, '<view style="display:block;$1</view>');
			// 	// 移除br标签
			// 	newContent = newContent.replace(/<br\s*[\/]?>/gi, "\r\n");
				return newContent;
			}
		},
		// 页面滑动监听
		onPageScroll(e) {
			this.scrollTop = e.scrollTop;
		},
    }
</script>

<style lang="scss" scoped>
.content {
	padding-bottom: 100rpx;
	.goods-info {
		width: 100%;
		background: #fff;
		padding: 14rpx 0 0 0;
		@include flexBox(space-between, null, column);
		.top {
			padding: 0 24rpx;
			margin-bottom: 16rpx;
			.goods-name {
				margin-top: 14rpx;
				color: #333333;
				font-weight: 600;
				font-size: 32rpx;
				@include ellipsis(2);
			}
			.goods-price {
				padding: 15rpx 0 10rpx;
				@include flexBox(space-between, center);
				.left {
					@include flexBox(flex-start, flex-end);
					.group {
						color: #9A9A9A;
						font-size: 24rpx;
						border-radius: 8rpx;
						padding: 0 4rpx;
						margin-right: 10rpx;
					}
					.symbol {
						color: $main-color-text;
						.text {
							font-size: 48rpx;
						}
					}
					.old {
						color: #696A6B;
						margin-left: 20rpx;
						text-decoration: line-through;
					}
				}
				.right {
					font-size: 24rpx;
					color: #999999;
					@include flexBox(center, center, column);
				}
			}
		}
		.deserved-money {
			width: 100%;
			padding: 10rpx 40rpx;
			border-radius: 8rpx;
			background: #FFF0F2;
			@include flexBox(flex-start, center);
			.image {
				width: 39rpx;
				height: 46rpx;
				margin-right: 10rpx;
			}
			.text {
				color: $main-color-text;
				font-weight: 600;
			}
		}
	}
	
	.section-module {
		padding: 0 20rpx;
		background: #fff;
		margin-top: 20rpx;
		.header {
			padding: 20rpx 10rpx;
			border-bottom: 1rpx solid #E0E0E0;
			@include flexBox(space-between, center);
			.left {
				font-weight: 600;
			}
			.right {
				color: #87888D;
				font-size: 26rpx;
			}
		}
		.section-content {
			padding: 20rpx 0;
			@include flexBox(space-between, center);
			.rule {
				width: 708rpx;
				height: 106rpx;
			}
			.left {
				.image {
					width: 56rpx;
					height: 56rpx;
					background: #000;
					border-radius: 50rpx;
					margin-left: -20rpx;
					border: 2rpx solid #fff;
					&:first-child {
						margin-left: 0;
					}
				}
			}
			.right {
				color: #87888D;
				font-size: 26rpx;
				.text {
					color: $main-color-text;
				}
			}
		}
	}
	.derail-msg {
		margin-top: 20rpx;
	}
	
	.footer-bar {
		position: fixed;
		bottom: 0;
		height: 100rpx;
		width: 100%;
		background: #fff;
		border-top: 1rpx solid #E0E0E0;
		padding: 10rpx 24rpx;
		@include flexBox(center);
		.btn {
			width: 100%;
			border-radius: 50rpx;
			font-size: 32rpx;
			font-weight: 600;
			&::after {
				border: none;
			}
			&.join {
				background: $main-color;
				color: #333333;
			}
		}
	}
}
</style>